﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmFacturaEdit : Form
    {
        const int iDefaultPAging = 10;
        DataSet dsReceptii = new DataSet();
        DataTable dtReceptii = new DataTable();
        SqlDataAdapter daReceptii = new SqlDataAdapter();
        SqlConnection conn;
        private int iPageCount;
        private int iMaxRec;
        private int iPageSize;
        private int iCurrentPage;
        private int iRecNo;


        public frmFacturaEdit(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;
        }

        private void frmFacturaEdit_Load(object sender, EventArgs e)
        {
            tbInregistrari.Text = iDefaultPAging.ToString();
            tbDataInceput.Value = Convert.ToDateTime("01/01/"+ System.DateTime.Today.Year.ToString());
            
            daReceptii = new SqlDataAdapter("SELECT * FROM receptii", conn);
            dsReceptii = new DataSet();

            //fill the dataset
            daReceptii.Fill(dsReceptii, "receptii");

            //set the source table
            CustomProcedures.conn = conn;
            dtReceptii = CustomProcedures.GETRECEPTII(string.Empty, string.Empty, string.Empty, string.Empty);//dsReceptii.Tables["receptii"];
            

            Filtreaza();


        }

        private void btnFata_Click(object sender, EventArgs e)
        {
            iCurrentPage = iCurrentPage + 1;

            if (iCurrentPage > iPageCount)
            {
                iCurrentPage = iPageCount;

                if (iRecNo == iMaxRec)
                {
                    MessageBox.Show("Te afli deja pe ultima pagina!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            LoadPage();

        }

        private void btnAplica_Click(object sender, EventArgs e)
        {
            try
            {
                iPageSize = int.Parse(tbInregistrari.Text.Trim());
            }
            catch (Exception ex)
            { 
            
            }
        }

        private void btnSpate_Click(object sender, EventArgs e)
        {
            

            if (iCurrentPage == iPageCount)
                iRecNo = iPageSize * (iCurrentPage - 2);

            iCurrentPage = iCurrentPage - 1;


            if (iCurrentPage < 1)
            {
                MessageBox.Show("Te afli deja pe prima pagina!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                iCurrentPage = 1;
                return;
            }
            else
                iRecNo = iPageSize * (iCurrentPage - 1);

            LoadPage();

        }

        private void LoadPage()
        {
        int i, iStartRec, iEndRec;
            DataTable dtTemp;
        DataRow dr;

         //Duplicate or clone the source table to create the temporary table.
         dtTemp = dtReceptii.Clone();

        if (iCurrentPage == iPageCount) 
            iEndRec = iMaxRec;
        else
            iEndRec = iPageSize * iCurrentPage;

        iStartRec = iRecNo;

        //Copy the rows from the source table to fill the temporary table.
        if (dtReceptii.Rows.Count > 0)
            for (i = iStartRec; i <= iEndRec - 1; i++)
            {
                dtTemp.ImportRow(dtReceptii.Rows[i]);
                iRecNo = iRecNo + 1;
            }
        else dtTemp = null;
        gridReceptii.DataSource = dtTemp;
        //DisplayPageInfo();
        lblPagina.Text = "Pagina  " + iCurrentPage.ToString() + " / " + iPageCount.ToString();

        }

        private void btnFiltreaza_Click(object sender, EventArgs e)
        {
            dtReceptii = CustomProcedures.GETRECEPTII(tbDataInceput.Value.ToString(), tbDataFinal.Value.ToString(), txtSerie.Text, txtNumar.Text);
            Filtreaza();

        }

        private void Filtreaza()
        {

            //Set the start and max records. 
            iPageSize = int.Parse(tbInregistrari.Text.Trim());

            iMaxRec = dtReceptii.Rows.Count;
            iPageCount = Convert.ToInt32(iMaxRec / iPageSize);

            // Adjust the page number if the last page contains a partial page.
            if ((iMaxRec % iPageSize) > 0)
                iPageCount = iPageCount + 1;


            //Initial seeings
            iCurrentPage = 1;
            iRecNo = 0;

            //Display the content of the current page.
            LoadPage();

        }

        private void btnIesire_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
             if (iCurrentPage == 1)
             {
                 MessageBox.Show("Te afli deja pe prima pagina!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
             }
    

            iCurrentPage = 1;
            iRecNo = 0;

            LoadPage();

        }

        private void btnLast_Click(object sender, EventArgs e)
        {

             // Check if you are already at the last page.
            if (iRecNo == iMaxRec) 
            {
                MessageBox.Show("Te afli deja pe ultima pagina!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 return;
            }
    

            iCurrentPage = iPageCount;

            iRecNo = iPageSize * (iCurrentPage - 1);

            LoadPage();

        }

        private void tbInregistrari_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)Keys.Back)
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                    e.Handled = true;
        }

        private void btnResetFiltru_Click(object sender, EventArgs e)
        {
            tbDataInceput.Value = tbDataFinal.Value = System.DateTime.Today;
            txtSerie.Text = txtNumar.Text = string.Empty;

            dtReceptii = CustomProcedures.GETRECEPTII(string.Empty, string.Empty, string.Empty, string.Empty);

            Filtreaza();
        }

        private void btnEditeaza_Click(object sender, EventArgs e)
        {
            Form form=null;

            form = frmReceptii.GetSingletonInstance(this.conn);
            
            ((frmReceptii)form).SetKeyValues((int)gridReceptii.CurrentRow.Cells[1].Value);
            form.MdiParent = this.MdiParent;
            form.Show();
            form.BringToFront();
        }

    }
}
